package com.dc.wifi.main.ui.fragment

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.hardware.usb.UsbManager
import android.os.Bundle
import android.os.Environment
import android.widget.Toast
import com.dc.netlib.base.BaseFragment
import com.dc.wifi.BR
import com.dc.wifi.R
import com.dc.wifi.databinding.FragmentMallBinding
import com.dc.wifi.main.model.MallViewModel
import com.dc.wifi.main.ui.activity.ImagePickerActivity
import com.dc.wifi.main.ui.activity.MonitorFileActivity
import com.dc.wifi.main.ui.activity.PayActivity
import com.dc.wifi.main.ui.activity.TestActivity
import com.dc.wifi.main.utils.STM32ControlTool



class MallFragment:BaseFragment<MallViewModel,FragmentMallBinding>(R.layout.fragment_mall, BR.mMallViewModel) {
    companion object {
        fun createFragment(bundle: Bundle = Bundle()): MallFragment {
            return MallFragment().apply {
                arguments = bundle
            }
        }
    }
    private lateinit var stm32ControlTool: STM32ControlTool

    override fun initData() {
        super.initData()
        val usbManager = requireContext().getSystemService(UsbManager::class.java)

        stm32ControlTool = STM32ControlTool(usbManager)
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        binding.btnScan.setOnClickListener {
//            findAndConnectDevice()
            //TestActivity
            startActivity(Intent(activity, TestActivity::class.java))

        }
        binding.webView.settings.javaScriptEnabled=true
//        binding.webView.loadUrl("https://www.baidu.com/")
        binding.webView.loadUrl("http://check.dstckj.com/userAgreement.html")
        binding.testSandbox.setOnClickListener {
            startActivity(Intent(activity, PayActivity::class.java))

        }




        binding.picturePicker.setOnClickListener{
            startActivity(Intent(activity, ImagePickerActivity::class.java))
        }
        binding.btnScanner.setOnClickListener {
            startActivity(Intent(activity, MonitorFileActivity::class.java))

        }

    }
    private fun findAndConnectDevice() {
        val usbManager = requireActivity().getSystemService(Context.USB_SERVICE) as UsbManager
        val deviceList = usbManager.deviceList

        val device = deviceList.values.find { device ->
            device.deviceName == "STM32 Virtual ComPort"
        }
        if (device != null && stm32ControlTool.connectDevice(device)) {
            Toast.makeText(requireContext(), "Device connected", Toast.LENGTH_SHORT).show()
            performAction("start_continuous")
        } else {
            Toast.makeText(requireContext(), "Device not found or connection failed", Toast.LENGTH_SHORT).show()
        }
    }

    private fun performAction(action: String) {
//        viewModelScope.launch(Dispatchers.IO) {
//            stm32ControlTool.performAction(action)
//            // 根据action处理响应，例如读取数据等
//        }
    }

    override fun onDestroy() {
        super.onDestroy()
        stm32ControlTool.disconnectDevice()
    }
}